`
https://leetcode.cn/problems/fruit-into-baskets/
`

/**
 * @param {number[]} fruits
 * @return {number}
 */
var totalFruit = function (fruits) {
  const n = fruits.length
  const exist = new Map()
  let res = 0
  let left = 0, right = 0

  while (right < n) {
    const t1 = fruits[right++]
    exist.set(t1, (exist.get(t1) || 0) + 1)

    // 缩小窗口
    while (exist.size > 2) {
      const t2 = fruits[left++]
      if (exist.get(t2) === 1) exist.delete(t2)
      else exist.set(t2, exist.get(t2) - 1)
    }

    // 更新答案
    res = Math.max(res, right - left)
  }

  return res
};